#!/bin/sh

# FILE=/opt/oracle/oradata/dbconfig/XE/sqlnet.ora
# until test -e "$FILE" ; do
#     echo "$FILE does not exist."
#     sleep 1s
# done

sed -i '$a DISABLE_OOB=ON' /opt/oracle/oradata/dbconfig/XE/sqlnet.ora

sqlplus -s / as sysdba <<EOF
    -- Create the datakit user. Replace the password placeholder with a secure password.
    CREATE USER datakit IDENTIFIED BY 123456;

    -- Grant access to the datakit user.
    GRANT CONNECT TO datakit;
    GRANT SELECT ON GV_\$PROCESS TO datakit;
    GRANT SELECT ON gv_\$sysmetric TO datakit;
    GRANT SELECT ON sys.dba_data_files TO datakit;
    GRANT SELECT ON sys.dba_tablespaces TO datakit;
    GRANT SELECT ON sys.dba_tablespace_usage_metrics TO datakit;

    -- Initialize testing data.
    CREATE TABLE students  ( student_id number(10) NOT NULL,  student_name varchar2(40) NOT NULL,  student_age varchar2(10)  );
    INSERT INTO students  (student_id, student_name, student_age)  VALUES  (3, 'Happy', '11');
    exit;
EOF

echo ""
echo "#########################"
echo "CREATED datakit USER!"
echo "#########################"

export LD_LIBRARY_PATH=/opt/instantclient:$LD_LIBRARY_PATH
/home/oracle/oracle --interval $DATAKIT_INTERVAL --host 0.0.0.0 --port 1521 --username datakit --password 123456 --service-name $ORACLE_SID --datakit-http-host $DATAKIT_HOST --datakit-http-port $DATAKIT_PORT
